package com.kgc.controller;


import com.aliyuncs.http.HttpRequest;
import com.kgc.pojo.User;
import com.kgc.service.GetUserInfo_wzlService;
import com.kgc.service.UserInfo_wzlService;
import com.kgc.service.UserService;
import com.kgc.util.Constants;
import com.kgc.util.RedisUtil;
import com.kgc.util.Result;
import com.kgc.util.SendPhoneMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 登录控制器
 * by-南宫问天 AND 王臻利
 */
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserInfo_wzlService userInfo_wzlService;

    @Autowired
    private GetUserInfo_wzlService getUserInfo_wzlService;

    @PostMapping("getLogin_TL")
    public Object login(@RequestParam String userPhone, @RequestParam String userPassword){
        List<User> user = userService.getUserByLogin(userPhone, userPassword);
        if(user.size() == 1){
            //登陆成功
            String token = UUID.randomUUID()+"";
            //需要用到用户id所以把userid存到result里
            Integer userId= user.get(0).getId();
            redisUtil.set(token,user.get(0),60*240);
            Map<String, Object> map = new HashMap<>();
            map.put("data",true);
            map.put("token",token);
            map.put("userId",userId);
            return new Result(map, "登录成功");
        }
        return new Result(false,"登录失败");
    }
    /*用户注册*/
    @PostMapping("InserUser_wzl")
    public Object InserUser_wzl(@RequestParam String phone,@RequestParam String password,
                                @RequestParam int userdetailid){
        List<User> user2 = getUserInfo_wzlService.getUserdetailInfo_wzl();
        /*循环遍历判断数据库中是否有相同的用户*/
        for (User user1:user2) {
            if (user1.getPhone().equals(phone)) {
                return new Result(false, "该用户已存在请从新输入");
            }
        }
        if(phone.length()!=11 || phone==null) {
            return new Result(false,"您输入的手机号有误请重新输入");
        }
        //用户新用户默认值1
        int count = userInfo_wzlService.InserUser(phone,password,userdetailid,1);
        //获取插入列的id
        User user =userInfo_wzlService.selectLastUser();
        //更新默认的userdetailId
        int  changesUserDetailId= userInfo_wzlService.updateUserid(user.getId(),user.getId());
        //userDetail表默认的图片 *默认的图片名称要包含在前端的upload文件夹中*
        String picpath="TB14UKCGQyWBuNjy0FpXXassXXa-54-54.png";
        //userDetail表默认的名称
        String nickname="含语墨轩";
        int  updateUserDetailinfo= userInfo_wzlService.InserUserDetailinfo_wzl(user.getId(),nickname,picpath);
        return new Result(true,"用户注册成功");
    }
    /*查询使用者的信息*/
    @PostMapping("selectUsr_info")
    public Object selectUsr_info(String phone){
        User users =userInfo_wzlService.selectUserinfo(phone);
        if(users!=null) {
            return new Result(users,"用户查找成功");
        }
        return  new Result(null,"没有该用户请重新输入");

    }

    /*发送手机信息*/
    @GetMapping("sendPhoneMessage_wzl")
    public Object sendPhoneMessage_wzl(String phone){
        SendPhoneMessage sendPhoneMessage =new SendPhoneMessage();
        String num =UUID.randomUUID()+"";
        String s=sendPhoneMessage.sendMessage(phone);//返回的验证码
        //将验证码存到redis中
        Constants.GetPHone_Num=num;
        redisUtil.set(num,s,60*15);//存入5分钟
        return new Result(true,"信息发送成功");
    }
    /*手机验证码验证*/
    @GetMapping("checkPhoneMessage_wzl")
    public Object checkPhoneMessage_wzl(HttpServletRequest request, String number){
        //获取对象并转化
        Object s = redisUtil.get(Constants.GetPHone_Num);
        String num=s.toString();
        if(number.equals(num)) {
            return new Result(true,"验证成功");
        }
        return new Result(false,"验证失败");
    }
    /*更改密码接口*/
    @GetMapping("updateUser_wzl")
    public Object updateUser_wzl(int id,String password){
        int num= userInfo_wzlService.updateUser(id,password);
        if(num>0) {
            return new Result(true, "修改成功");
        }
        return new Result(true, "修改失败");
    }

    @RequestMapping("loginOut_CY")
    public Object loginOut(HttpServletRequest request) {
        String token = request.getHeader("token");
        redisUtil.del(token);
        return new Result("true", "注销成功");
    }
}
